'use strict';
const {parseTime, parseNum} = require("../../utils");
const {getDefaultModelConfig} = require("../../utils/common");

module.exports = app => {
    const { STRING, INTEGER, DATE, TINYINT, DECIMAL, BIGINT } = app.Sequelize;
    const Recharge = app.model.define(
        'Recharge',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            balance_record_id: BIGINT,
            order_no: STRING(255),
            user_id: BIGINT,
            status: {
                type: STRING,
                defaultValue: 'pending', // pending 待支付, paid 已支付, refunding 退款中, refunded 已退款, cancelled 已取消
            },
            remark: STRING(255),
            payment_amount: {
                type: DECIMAL(10, 2),
                defaultValue: 0.00,
                get() {
                    return parseNum(this.getDataValue('payment_amount'));
                }
            },
            payment_time: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('payment_time'));
                }
            },
            time_expire: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('time_expire'));
                }
            },
            transaction_id: STRING(255),
            prepay_id: STRING(255),
            refund_no: STRING(255),
            refund_transaction_id: STRING(255),
            refund_status: {type: TINYINT, defaultValue: -1},
            refund_time: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('refund_time'));
                }
            },
            refund_success_time: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('refund_success_time'));
                }
            },
            refund_reason: STRING(255),
            refund_remark: STRING(255),
            refund_result: STRING(255),
            refund_amount: {type: DECIMAL(10, 2), defaultValue: 0.00},
            cancel_time: {
                type: DATE,
                get() {
                    return parseTime(this.getDataValue('cancel_time'));
                }
            },
            handler_id: BIGINT,
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'recharge',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );

    Recharge.associate = function() {
        app.model.Common.Recharge.belongsTo(app.model.Business.User, {as: 'user', foreignKey: 'user_id'});
    }
    return Recharge
};
